package own.stu.jobgib.playown.io.fileRead;

import org.apache.commons.lang3.StringUtils;

import java.nio.ByteBuffer;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class Constant {

    public static final String FILE_NAME = "/Users/qim/Desktop/tmp/noUse/data.txt";

    /**
     * byteBuffer 转 byte数组
     *
     * @param buffer
     * @return
     */
    public static byte[] bytebuffer2ByteArray(ByteBuffer buffer) {
        //重置 limit 和postion 值
        int position = buffer.position();
        buffer.flip();
        //获取buffer中有效大小
        int len = buffer.limit() - buffer.position();

        byte[] bytes = new byte[len];

        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = buffer.get();
        }
        buffer.position(position);

        return bytes;
    }

    public static class SplitData {

        public static void splitLine(String lineData, Map<String, AtomicInteger> countMap) {
            String[] arr = lineData.split(",");
            for (String str : arr) {
                if (StringUtils.isEmpty(str)) {
                    continue;
                }
                countMap.computeIfAbsent(str, s -> new AtomicInteger(0)).getAndIncrement();
            }
        }
    }
}
